package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logi-flow/cache"
	"logi-flow/model"
	"strconv"
	"time"
)

func PathAdd(c *gin.Context) {
	pathCode := c.PostForm("path_code")
	pathName := c.PostForm("path_name")
	pathArea := c.PostForm("path_area")
	if pathCode == "" || pathName == "" || pathArea == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不为空",
		})
		return
	}
	path := model.Path{
		PathCode: pathCode,
		PathName: pathName,
		PathArea: pathArea,
	}
	err := model.DB.Create(&path).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func PathOrderAdd(c *gin.Context) {
	orderCode := c.PostForm("order_code")
	orderNum, _ := strconv.Atoi(c.PostForm("order_num"))
	orderPrice, _ := strconv.Atoi(c.PostForm("order_price"))
	pid, _ := strconv.Atoi(c.PostForm("pid"))
	if orderCode == "" || orderNum == 0 || orderPrice == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	pathOrder := model.PathOrder{
		OrderCode:  orderCode,
		OrderNum:   orderNum,
		OrderPrice: orderPrice,
		Pid:        pid,
	}
	err := model.DB.Create(&pathOrder).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func PathOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	/*
			SELECT
		  path_orders.id as PathOrderId,
		  paths.path_code as PathCode,
		  paths.path_name as PathName,
		  paths.path_area as PathArea,
		  path_orders.order_code as OrderCode,
		  path_orders.order_num as OrderNum,
		  path_orders.order_price as OrderPrice
		FROM
		  path_orders
		LEFT JOIN paths ON path_orders.pid = paths.id;
	*/
	var list []model.PathOrderList
	redis_key := "path_order_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("path_orders").
			Select("path_orders.id as PathOrderId",
				"paths.path_code as PathCode",
				"paths.path_name as PathName",
				"paths.path_area as PathArea",
				"path_orders.order_code as OrderCode",
				"path_orders.order_num as OrderNum",
				"path_orders.order_price as OrderPrice",
			).
			Joins("LEFT JOIN paths ON path_orders.pid = paths.id").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var total int64
	model.DB.Model(&model.PathOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  list,
		"total": total,
	})
	return
}

func PathOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var list model.PathOrderList
	redis_key := "path_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("path_orders").
			Select("path_orders.id as PathOrderId",
				"paths.path_code as PathCode",
				"paths.path_name as PathName",
				"paths.path_area as PathArea",
				"path_orders.order_code as OrderCode",
				"path_orders.order_num as OrderNum",
				"path_orders.order_price as OrderPrice",
			).
			Joins("LEFT JOIN paths ON path_orders.pid = paths.id").
			Where("path_orders.id = ?", id).
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
